package com.lagou.lagouhomework06.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @author mcl
 * @date 2021/7/19 9:03 上午
 * @description
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ApplicationContext applicationContext;

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpSession httpSession) {
        if("admin".equals(username) && "admin".equals(password)) {
            httpSession.setAttribute("username",username + System.currentTimeMillis());
            return "success";
        }
        return "failed";
    }

    @GetMapping("/userInfo")
    public String userInfo(HttpSession httpSession) {
        Environment bean = applicationContext.getBean(Environment.class);
        String serverPort = bean.getProperty("server.port");
        return "ServerPort: " + serverPort + ", Session: " + String.valueOf(httpSession.getAttribute("username"));
    }
}
